use std::env;
use std::ffi::OsStr;
use std::path::PathBuf;

use libbpf_cargo::SkeletonBuilder;

fn main() {
    build_bpf("srv6_router");
}

fn build_bpf(name: &str) {
    let src = format!("src/bpf/{name}.bpf.c");
    let out = PathBuf::from(
        env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR must be set in build script"),
    )
        .join("src")
        .join("bpf")
        .join( format!("{name}.skel.rs"));

    let arch = env::var("CARGO_CFG_TARGET_ARCH")
        .expect("CARGO_CFG_TARGET_ARCH must be set in build script");

    SkeletonBuilder::new()
        .source(src.clone())
        .clang_args([
            OsStr::new("-Wno-compare-distinct-pointer-types"),
            OsStr::new("-I"),
            vmlinux::include_path_root().join(arch).as_os_str(),
        ])
        .build_and_generate(&out)
        .unwrap();
    println!("cargo:rerun-if-changed={src}");
}